//
//  LGChangePhoneController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/14.
//  更换手机号

#import "LGChangePhoneController.h"

#import "LGAccountInputView.h"

@interface LGChangePhoneController ()
/**   */
@property (nonatomic , strong)LGAccountInputView *phoneView;
/**   */
@property (nonatomic , strong)LGAccountInputView *codeView;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;

/**   */
@property (nonatomic , strong)NSTimer *timer;
/**   */
@property (nonatomic , assign)NSInteger seconds;

@end

@implementation LGChangePhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"更换手机号";
    [self.view addSubview:self.phoneView];
    [self.view addSubview:self.codeView];
    [self.view addSubview:self.sureBtn];
}

//获取验证码
-(void)getSmsCodeAction:(UIButton *)sender{
    NSString *phoneNum = [self.phoneView.contenTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if([self checkTelNumber:phoneNum] == NO){
        [TooltipView showMessage:@"请输入正确的手机号" offset:0];
        return;
    }
    [RequestUtil withPOST:@"/doctor/my.setting/updateMobileSms" parameters:@{@"mobile":phoneNum} withSign:NO success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.seconds = 60;
            [self.timer invalidate];
            self.timer = nil;
            self.codeView.actionBtn.selected = YES;
            self.codeView.actionBtn.userInteractionEnabled = NO;
            [self.codeView.actionBtn setTitle:[NSString stringWithFormat:@"重新发送(%ld)",self.seconds] forState:UIControlStateNormal];
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
}

//验证码倒计时
-(void)countDownAction{
    self.seconds--;
    [self.codeView.actionBtn setTitle:[NSString stringWithFormat:@"重新发送(%ld)",self.seconds] forState:UIControlStateNormal];
    if (self.seconds == 0) {
        self.codeView.actionBtn.selected = NO;
        self.codeView.actionBtn.userInteractionEnabled = YES;
        [self.codeView.actionBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
    }
}

//确定
-(void)sureBtnAction{
    NSString *phoneNum = [self.phoneView.contenTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    [RequestUtil withPOST:@"/doctor/my.setting/updateMobile" parameters:@{@"mobile":phoneNum,@"code":self.codeView.contenTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [self.navigationController popViewControllerAnimated:YES];
            [TooltipView showMessage:@"更换成功" offset:0];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}

//判断手机格式
- (BOOL)checkTelNumber:(NSString *)telNumber{
    NSString *pattern = @"^1+[0123456789]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}


-(LGAccountInputView *)phoneView{
    if(!_phoneView){
        _phoneView = [[LGAccountInputView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(24), Screen_W-viewPix(32), viewPix(56))];
        [_phoneView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _phoneView.backgroundColor = [UIColor whiteColor];
        _phoneView.type = @"phone";
        _phoneView.hasBorder = NO;
    }
    return _phoneView;
}

-(LGAccountInputView *)codeView{
    if(!_codeView){
        _codeView = [[LGAccountInputView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.phoneView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(56))];
        [_codeView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _codeView.backgroundColor = [UIColor whiteColor];
        _codeView.type = @"code";
        _codeView.hasBorder = NO;
        [_codeView.actionBtn addTarget:self action:@selector(getSmsCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeView;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" topMargin:kMaxY(self.codeView.frame)+viewPix(140)];
        [_sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
